<template>
  <div class="wrap">
    <div class="header">
      <div class="left">
      </div>
      <div class="right">
        <div style="display: inline-block; width: 300px;">
          <el-input
            size="small"
            style="width: 200px; margin-right: 10px; margin-bottom: 10px"
            v-model="queryParams.keyword"
            :placeholder="$t('common.Search for store or username')"
            @keyup.enter.native="changeOption(true)"
            clearable @clear="changeOption(false, 'clear')"
          ></el-input>
          <el-button size="small" type="primary" @click="changeOption(true)">{{ $t('common.search') }}</el-button>
        </div>
      </div>
    </div>
    <span>
      {{$t('order.cutOff.履约时间表')}} <span v-if="!loading">({{$t('order.cutOff.更新时间')}}：{{saveDate}})</span>
    </span>
    <el-button class="mgl20" size="small" type="info" @click="saveCutOff()">{{ $t('order.cutOff.更新履约时间表') }}</el-button>
    <div class="content">
      <el-table
        v-loading="loading"
        :data="data"
        border
        @sort-change="handleSortChange"
        :default-sort="defaultSort"
      >
        <el-table-column
          prop="userName"
          :label="$t('order.cutOff.用户名')"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="nickname"
          :label="$t('order.cutOff.店铺')"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="cutoff"
          :label="$t('order.cutOff.截单时间')"
          align="center"
          min-width="100"
          sortable="custom"
          :sort-orders="sortOrders"
        >
        </el-table-column>
        <el-table-column
          prop="maximumTime"
          :label="$t('order.cutOff.送达时间')"
          align="center"
          min-width="100"
          sortable="custom"
          :sort-orders="sortOrders"
        >
        </el-table-column>
        <el-table-column
          prop="processingTime"
          :label="$t('order.cutOff.履约时间')"
          align="center"
          min-width="100"
          sortable="custom"
          :sort-orders="sortOrders"
        >
        </el-table-column>
      </el-table>
      <div class="page-wrap mgt10">
        <el-pagination
          @current-change="getData"
          :current-page.sync="queryParams.page"
          :page-size="queryParams.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {getAccountCutOffListData, saveCutOff, cutOffCollectIsDone} from "@/api/order";
export default {
  name: "cutOffList",
  data(){
    return {
      queryParams:{
        page: 1,
        pageSize: 20,
        keyword: '',
        orderBy: '',
        orderField: '',
      },
      data: [],
      loading: false,
      total: 0,
      saveDate: '',
      is_search: false,
      sortOrders: ['descending', 'ascending', null],
      defaultSort:{
        prop: 'processingTime',
        order: 'ascending'
      },
      cutOffTimer: null,
    }
  },
  created() {
    this.queryParams.orderBy = this.defaultSort.order
    this.queryParams.orderField = this.defaultSort.prop
    this.getData()
  },
  methods: {
    getData(){
      this.loading = true
      getAccountCutOffListData(this.queryParams)
        .then(res=>{
          this.loading = false
          this.data = res.data
          this.total = res.info.total
          this.saveDate = res.info.save_date
        })
    },
    handleSizeChange(val){
      this.queryParams.pageSize = val;
      this.queryParams.page = 1;
      this.getData();
    },
    //排序
    handleSortChange(column){
      this.queryParams.orderBy = ''
      this.queryParams.orderField = ''
      if(column.order){
        this.queryParams.orderBy = column.order
        this.queryParams.orderField = column.prop
      }
      this.queryParams.page = 1;
      this.getData()
    },
    changeOption(is_search = false, type = ''){
      if (is_search === true) {
        //判断是否使用过搜索按钮
        this.is_search = true
      }
      if (type === 'clear') {
        if (this.is_search === false) {
          return
        } else {
          this.is_search = false
        }
      }
      this.queryParams.page = 1;
      this.getData()
    },
    saveCutOff(){
      let title = this.$t('order.cutOff.确认更新履约时间表？');
      let html = `${this.$t('order.cutOff.当数据量较大时，系统可能需要数分钟时间完成更新，期间您可进行其他操作')}<br/><span style="color: #FF0000">${this.$t('order.cutOff.更新结果请以页面左侧的“更新时间”为准')}</span>`
      this.$confirm(html, title, {
        dangerouslyUseHTMLString: true,
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonClass: this.$t('common.cancel')
      })
        .then(res=>{
          // 点击了确定
          saveCutOff()
            .then(res=>{
              this.createTimer(); // 无论成功失败都创建定时器
              if(res.error === 0){
                this.$successMessage(res.message)
              }else{
                this.$errorMessage(res.message)
              }
            })
        })
        .catch(res=>{
          // 点击了取消
        });
    },
    createTimer(){
      if(this.cutOffTimer){
        return;
      }
      this.cutOffTimer = setInterval(()=>{
        cutOffCollectIsDone()
          .then(res=>{
            if(res.data.isDone){
              this.$router.go(0)
            }
          })
      },3000);
    },
    // 清除定时器
    beforeDestroy() {
      if (this.cutOffTimer) {
        clearInterval(this.cutOffTimer)
      }
    },
  },
}
</script>

<style scoped lang="scss">
.wrap {
  padding: 30px 30px;
  .header{
    display: flex;
    justify-content: space-between;
  }
  .content{
    padding: 20px 0 0;
  }
}
</style>
